package com.swan.controllers;

import java.util.List;
import java.util.Optional;

import com.swan.beans.ClassBase;
import com.swan.models.ClassDao;
import com.swan.utils.CellFactory;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;

/**
 * ClassMgtController
 * 
 * @author xyh
 * @param <T>
 * @date 20230426
 *
 */

public class ClassMgtController {
	@FXML
	private Button DelBtn;

	// add class fields
	@FXML
	private TextField IdTextField;

	@FXML
	private TextField TitleTextField;

	@FXML
	private TextField SubjectTextField;

	@FXML
	private TextField DescriptionTextField;

	@FXML
	private TextField CourseNoTextField;

	@FXML
	private TextField SectionTextField;

	@FXML
	private TextField HoursTextField;

	@FXML
	private TextField CrnTextField;

	@FXML
	private TextField TermTextField;

	@FXML
	private TextField InstructorTextField;

	@FXML
	private TextField SeatsTextField;

	@FXML
	private TextField TimeFrameTextField;

	@FXML
	private TextField CampusTextField;

	@FXML
	private TextField AttrTextField;

	@FXML
	private TableView<ClassBase> TableView;

	// Table view fields
	@FXML
	private TableColumn<ClassBase, Boolean> SelectTableColumn;

	@FXML
	private TableColumn<ClassBase, Integer> IdTableColumn;

	@FXML
	private TableColumn<ClassBase, String> TitleTableColumn;

	@FXML
	private TableColumn<ClassBase, String> SubjectTableColumn;

	@FXML
	private TableColumn<ClassBase, String> DescriptionTableColumn;

	@FXML
	private TableColumn<ClassBase, String> CourseNoTableColumn;

	@FXML
	private TableColumn<ClassBase, Integer> SectionTableColumn;

	@FXML
	private TableColumn<ClassBase, Integer> HoursTableColumn;

	@FXML
	private TableColumn<ClassBase, Integer> CrnTableColumn;

	@FXML
	private TableColumn<ClassBase, Integer> TermTableColumn;

	@FXML
	private TableColumn<ClassBase, String> InstructorTableColumn;

	@FXML
	private TableColumn<ClassBase, Integer> SeatsTableColumn;

	@FXML
	private TableColumn<ClassBase, String> TimeFrameTableColumn;

	@FXML
	private TableColumn<ClassBase, String> CampusTableColumn;

	@FXML
	private TableColumn<ClassBase, String> AttrTableColumn;

	private final ObservableList<ClassBase> data = FXCollections.observableArrayList();

	private ClassDao dao = new ClassDao();

	@FXML
	public void onDelete(ActionEvent event) {
		delete();
	}

	@FXML
	public void onAdd(ActionEvent event) {
		if (TitleTextField.getText() != null) {
			System.out.println(TitleTextField.getText());

			String title = TitleTextField.getText();
			String subject = SubjectTextField.getText();
			String description = DescriptionTextField.getText();
			String courseNo = CourseNoTextField.getText();
			String section = SectionTextField.getText();
			String hours = HoursTextField.getText();
			String crn = CrnTextField.getText();
			String term = TermTextField.getText();
			String instructor = InstructorTextField.getText();
			String seats = SeatsTextField.getText();
			String timeFrame = TimeFrameTextField.getText();
			String campus = CampusTextField.getText();
			String attr = AttrTextField.getText();

			ClassBase row = new ClassBase();
			row.setTitle(title);
			row.setSubject(subject);
			row.setDescription(description);
			row.setCourseNumber(courseNo);
			row.setSection(Integer.valueOf(section));
			row.setHours(Integer.valueOf(hours));
			row.setCrn(Integer.valueOf(crn));
			row.setTerm(Integer.valueOf(term));
			row.setInstructor(instructor);
			row.setSeats(Integer.valueOf(seats));
			row.setTimeFrame(timeFrame);
			row.setCampus(campus);
			row.setAttribute(attr);
			dao.addClass(row);
			
			if (row.getId() > 0) {
				System.out.println("add class success!");
				data.add(row);
			}

			TitleTextField.clear();
			SubjectTextField.clear();
			DescriptionTextField.clear();
			CourseNoTextField.clear();
			SectionTextField.clear();
			HoursTextField.clear();
			CrnTextField.clear();
			TermTextField.clear();
			InstructorTextField.clear();
			SeatsTextField.clear();
			TimeFrameTextField.clear();
			CampusTextField.clear();
			AttrTextField.clear();
		}
	}

	private boolean delete() {
		int size = data.size();
		if (size <= 0) {
			return false;
		}
		for (int i = size - 1; i >= 0; i--) {
			ClassBase row = data.get(i);
			if (row.getSelected()) {
				// confirm delete
				Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
				alert.setContentText("confirm delete class ?");
				Optional<ButtonType> showAndWait = alert.showAndWait();
				if (showAndWait.get() == ButtonType.OK) {
					if (row.getId() > 0) {
						dao.removeClass(row.getId());
						System.out.println("delete class success!");
					}
				}
			}
		}
		return true;
	}

	@FXML
	private void initialize() {
		// table view
		// set select column ui
		SelectTableColumn
				.setCellFactory(CellFactory.tableCheckBoxColumn(new Callback<Integer, ObservableValue<Boolean>>() {
					@Override
					public ObservableValue<Boolean> call(Integer index) {
						final ClassBase row = TableView.getItems().get(index);
						ObservableValue<Boolean> ret = new SimpleBooleanProperty(row, "selected", row.getSelected());
						ret.addListener(new ChangeListener<Boolean>() {
							@Override
							public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue,
									Boolean newValue) {
								row.setSelected(newValue);
							}
						});
						return ret;
					}
				}));

		// set column value
		SelectTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, Boolean>("selected"));
		IdTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, Integer>("id"));
		TitleTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, String>("title"));
		SubjectTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, String>("subject"));
		DescriptionTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, String>("description"));
		CourseNoTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, String>("courseNumber"));
		SectionTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, Integer>("section"));
		HoursTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, Integer>("hours"));
		CrnTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, Integer>("crn"));
		TermTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, Integer>("term"));
		InstructorTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, String>("instructor"));
		SeatsTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, Integer>("seats"));
		TimeFrameTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, String>("timeFrame"));
		CampusTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, String>("campus"));
		AttrTableColumn.setCellValueFactory(new PropertyValueFactory<ClassBase, String>("attribute"));

		// set table view
		List<ClassBase> list = dao.list();
		System.out.println("list class success!");
		data.addAll(list);

		TableView.setItems(data);
		TableView.setEditable(true);

	}

}
